import 'package:flutter/widgets.dart';
import 'package:whale/pages/im/model/im.dart';

import '../../../helper/utils/util.dart';
import 'index.dart';

class ImController extends GetxController {
  ImController();

  final state = ImState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.initialization();
    state.imListMd = Get.arguments;
  }

  void setupData() {
    Map<String, dynamic> fromJson = state.imListMd.fromJson;
    state.list = [
      ImModel(
        id: '001',
        fromJson: fromJson,
        msgJson: state.imListMd.msgJson,
      ),
      ImModel(
        id: '002',
        fromJson: fromJson,
        msgJson: {
          'id': '00002',
          'type': 'text',
          'content': '[色]好的，已下发',
          'time': '15:50',
          'isRemind': '1',
          'isRead': '0',
          'userId': '0000',
        },
      ),
      ImModel(
        id: '003',
        fromJson: fromJson,
        msgJson: {
          'id': '00003',
          'type': 'system',
          'content': '2023.9.11',
        },
      ),
      ImModel(
        id: '004',
        fromJson: fromJson,
        msgJson: {
          'id': '00004',
          'type': 'text',
          'content': 'im聊天系统开发，[旺柴]2023[加油]年9月11日开始，预计一周结束主框架开发💪🏻',
          'time': '9:40',
          'isRemind': '1',
          'isRead': '1',
          'userId': '0000',
        },
      ),
      ImModel(
        id: '005',
        fromJson: fromJson,
        msgJson: {
          'id': '00005',
          'type': 'image',
          'content':
              'https://pics6.baidu.com/feed/f9dcd100baa1cd11629a9979bc491af0c3ce2d26.jpeg@f_auto?token=39aeb99059f9aa38e636ab068939d77d',
          'thumb':
              'https://pics6.baidu.com/feed/f9dcd100baa1cd11629a9979bc491af0c3ce2d26.jpeg@f_auto?token=39aeb99059f9aa38e636ab068939d77d',
          'w': 390,
          'h': 203,
          'time': '9:40',
          'isRemind': '1',
          'isRead': '1',
          'userId': '0000',
        },
      ),
      ImModel(
        id: '006',
        fromJson: fromJson,
        msgJson: {
          'id': '00006',
          'type': 'voice',
          'content':
              'https://pics6.baidu.com/feed/f9dcd100baa1cd11629a9979bc491af0c3ce2d26.jpeg@f_auto?token=39aeb99059f9aa38e636ab068939d77d',
          'time': '9:40',
          'isRemind': '1',
          'isRead': '1',
          'userId': '0000',
          'size': 2048,
          'seconds': 16,
        },
      ),
    ];
    update();
  }

  void sendEvent(String s) {
    var m = ImModel.createText(s);
    state.list?.add(m);
    update();
    scrollToEnd();
  }

  void sendVoiceEvent(int seconds) {
    var m = ImModel.createVoice('url', seconds: seconds);
    state.list?.add(m);
    update();
    scrollToEnd();
  }

  void sendPictureEvent({
    required String path,
    double? width,
    double? height,
  }) {
    var m = ImModel.createPicture(
      path,
      width: width,
      height: height,
    );
    state.list?.add(m);
    update();
    scrollToEnd();
  }

  void sendLocationEvent({
    required String path,
    double? width,
    double? height,
  }) {
    var m = ImModel.createMap(path,
        lng: 113.726689, lat: 34.845177, title: '新科技市场');
    state.list?.add(m);
    update();
    scrollToEnd();
  }

  void sendCardEvent({
    required String path,
  }) {
    var m = ImModel.createCard(path,
        title: '卡片分享标题',
        des: '卡片分享描述',
        image:
            'https://pics0.baidu.com/feed/2934349b033b5bb5e048c9055e283c34b400bcb3.jpeg@f_auto?token=aabc02a67cf50b0fef3586e2dc74de98',
        sourceIcon:
            'https://gips0.baidu.com/it/u=2404911338,3690218263&fm=3012&app=3012&autime=1714028039&size=b200,200',
        sourceName: '每日经济新闻');
    state.list?.add(m);
    update();
    scrollToEnd();
  }

  void resend(ImModel m) async {
    m.msg.status = IMMsgStatus.sending;
    update(['refresh']);
    await Future.delayed(const Duration(seconds: 1));
    m.msg.status = IMMsgStatus.success;
    update(['refresh']);
  }

  void scrollToEnd() {
    Future.delayed(const Duration(milliseconds: 400)).then((value) {
      state.sc.animateTo(state.sc.position.maxScrollExtent,
          duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    setupData();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
